<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织结构" name="first" class="tab-pane">
            <el-row type="flex" class="row-bg" justify="space-between">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addHandle('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-tree
              class="departments-tree"
              :data="treeData"
              default-expand-all
              :props="defaultProps"
            >
              <template slot-scope="{ data }">
                <el-row
                  type="flex"
                  class="row-bg"
                  justify="space-between"
                  style="height: 50px; width: 100%"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addHandle(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editHandle(data.id)"
                            >修改子部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data.children? false:true"
                              @click.native="removeHandle(data.id)"
                            >删除子部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row></el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- //弹窗出现 -->
        <el-dialog
          :title="isEdit?'编辑部门':'添加部门'"
          :visible.sync="isShow"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <dialogPage v-if="isShow" :id="id" :isedit="isEdit" :origin-list="originList" @confirmFn="confirmFn" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getTreeData } from '@/utils'
import { getDepartmentsAPI, delOneDeptAPI } from '@/api/departments'
import dialogPage from './dialogPage.vue'
export default {
  name: 'Department',
  components: {
    dialogPage
  },
  data() {
    return {
      activeName: 'first', // 选中的导航栏
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      treeData: [], // 树形数据
      isShow: false, // 弹窗是否显示
      isEdit: false, // 是否为编辑部门
      id: '', // 添加和编辑时传入的形参
      oneDept: {}, // 某一个部门的数据
      originList: []// 用于添加部门时的校验
    }
  },
  created() {
    this.getTreeData()
  },
  methods: {
    // 获取树形结构数据
    async getTreeData() {
      const res = await getDepartmentsAPI()
      this.treeData = getTreeData(res.data.depts, '')
      this.originList = res.data.depts.map(item => (
        {
          id: item.id,
          code: item.code,
          pid: item.pid,
          name: item.name
        }
      ))
    },
    // 添加部门
    addHandle(id) {
      this.isShow = true
      this.isEdit = false
      this.id = id
    },
    // 编辑部门
    editHandle(id) {
      this.isEdit = true
      this.isShow = true
      this.id = id
    },
    // 表单点击确定按钮
    confirmFn() {
      this.isShow = false // 关闭弹窗
      this.getTreeData() // 重新获取数据
    },
    // 表单点击取消按钮
    cancelFn() {
      this.isShow = false // 关闭弹窗
    },
    // 表单点击删除按钮触发事件
    removeHandle(id) {
      // 确认弹窗出现
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 调用接口
        const res = await delOneDeptAPI(id)
        console.log(res, 3333333333333333)
        this.getTreeData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
